package cloud.tianai.mate.captcha.interceptor.impl;

import cloud.tianai.captcha.common.AnyMap;
import cloud.tianai.captcha.common.response.ApiResponse;
import cloud.tianai.captcha.interceptor.Context;
import cloud.tianai.captcha.validator.common.model.dto.ImageCaptchaTrack;
import cloud.tianai.captcha.interceptor.CaptchaInterceptor;
import cloud.tianai.mate.captcha.validator.common.util.TrackUtils;

import java.util.List;

/**
 * @Author: 天爱有情
 * @date 2023/1/16 18:34
 * @Description 用于提前生成特征值
 */
public class TrackFeaturesGenerator implements CaptchaInterceptor {

    public static final String TRACK_FEATURES_KEY = "track_features";

    @Override
    public String getName() {
        return "track_features_gen";
    }

    @Override
    public ApiResponse<?> afterValid(Context context, String type, ImageCaptchaTrack imageCaptchaTrack, AnyMap validData, ApiResponse<?> basicValid) {
        if (!basicValid.isSuccess()) {
            return context.getGroup().afterValid(context, type, imageCaptchaTrack, validData, basicValid);
        }
        List<Double> features = context.getData(TrackFeaturesGenerator.TRACK_FEATURES_KEY, List.class);
        if (features == null) {
            features = TrackUtils.features(imageCaptchaTrack.getTrackList());
            context.putData(TrackFeaturesGenerator.TRACK_FEATURES_KEY, features);
        }
        return ApiResponse.ofSuccess();
    }
}
